using System;
using System.Collections.Generic;
using System.Xml.Linq;

namespace Coderoom.ScrumPlanner.Utilities
{
	public static class XmlExtensions
	{
		public static IEnumerable<XElement> ImmediateDescendants(this XDocument data, XName name)
		{
			// ReSharper disable PossibleNullReferenceException
			return data.Root.Descendants(name);
			// ReSharper restore PossibleNullReferenceException
		}

		public static bool HasAttribute(this XElement element, XName name)
		{
			return (element.Attribute(name) != null && !string.IsNullOrEmpty(element.ReadAttribute(name)));
		}

		public static string ReadAttribute(this XElement element, XName name)
		{
			// ReSharper disable PossibleNullReferenceException
			return element.Attribute(name).Value;
			// ReSharper restore PossibleNullReferenceException
		}

		public static Guid ReadAttributeAsGuid(this XElement element, XName name)
		{
			return new Guid(element.ReadAttribute(name));
		}

		public static string ReadChildValue(this XContainer element, XName name)
		{
			// ReSharper disable PossibleNullReferenceException
			return element.Element(name).Value;
			// ReSharper restore PossibleNullReferenceException
		}
	}
}